package com.copa.l876;

import java.util.HashMap;

/**
 * @author copa
 * @createDate 2022-09-05 10:51
 * @function hashmap存储法
 */
public class Test1 {

    public ListNode middleNode(ListNode head) {
        HashMap<Integer, ListNode> map = new HashMap<>();
        int count = 1;
        while (head != null) {
            map.put(count, head);
            head = head.next;
            count++;
        }
        return map.get(map.size() / 2 + 1);
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(2);
        head.next.next = new ListNode(3);
        head.next.next.next = new ListNode(4);
        head.next.next.next.next = new ListNode(5);
        //head.next.next.next.next.next = new ListNode(6);
        ListNode listNode = new Test1().middleNode(head);
        while (listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }
}
